import { EquipmentSlots, CalculatedStats, InventoryItem, EquipmentType } from '../types/game';

/**
 * 装备管理工具类
 */
export class EquipmentManager {
  /**
   * 计算装备后的总属性
   */
  static calculateStats(equipment: EquipmentSlots): CalculatedStats {
    const stats: CalculatedStats = {
      totalAttack: 0,
      totalDefense: 0,
      totalAccuracy: 0,
      totalSpeed: 0,
      inventoryCapacity: 10, // 基础容量
      ammoCapacity: 0,
    };

    // 遍历所有装备槽位
    Object.values(equipment).forEach(item => {
      if (item && item.equipmentStats) {
        const equipStats = item.equipmentStats;
        
        stats.totalAttack += equipStats.attack || 0;
        stats.totalDefense += equipStats.defense || 0;
        stats.totalAccuracy += equipStats.accuracy || 0;
        stats.totalSpeed += equipStats.speed || 0;
        stats.inventoryCapacity += equipStats.capacity || 0;
        stats.ammoCapacity += equipStats.ammoCapacity || 0;
      }
    });

    return stats;
  }

  /**
   * 检查物品是否可以装备到指定槽位
   */
  static canEquipItem(item: InventoryItem, slotType: EquipmentType): boolean {
    return item.type === 'equipment' && item.equipmentType === slotType;
  }

  /**
   * 装备物品到指定槽位
   */
  static equipItem(
    currentEquipment: EquipmentSlots, 
    item: InventoryItem, 
    slotType: EquipmentType
  ): { 
    newEquipment: EquipmentSlots; 
    unequippedItem: InventoryItem | null;
    success: boolean;
  } {
    if (!this.canEquipItem(item, slotType)) {
      return {
        newEquipment: currentEquipment,
        unequippedItem: null,
        success: false
      };
    }

    const newEquipment = { ...currentEquipment };
    let unequippedItem: InventoryItem | null = null;

    // 获取对应的槽位键名
    const slotKey = this.getSlotKey(slotType);
    
    // 如果槽位有装备，先卸下
    if (newEquipment[slotKey]) {
      unequippedItem = newEquipment[slotKey];
    }

    // 装备新物品
    newEquipment[slotKey] = item;

    return {
      newEquipment,
      unequippedItem,
      success: true
    };
  }

  /**
   * 卸下指定槽位的装备
   */
  static unequipItem(
    currentEquipment: EquipmentSlots, 
    slotType: EquipmentType
  ): {
    newEquipment: EquipmentSlots;
    unequippedItem: InventoryItem | null;
  } {
    const newEquipment = { ...currentEquipment };
    const slotKey = this.getSlotKey(slotType);
    const unequippedItem = newEquipment[slotKey];
    
    newEquipment[slotKey] = null;

    return {
      newEquipment,
      unequippedItem
    };
  }

  /**
   * 获取装备类型对应的槽位键名
   */
  private static getSlotKey(slotType: EquipmentType): keyof EquipmentSlots {
    switch (slotType) {
      case EquipmentType.HEAD:
        return 'head';
      case EquipmentType.BODY:
        return 'body';
      case EquipmentType.PRIMARY_WEAPON:
        return 'primaryWeapon';
      case EquipmentType.SECONDARY_WEAPON:
        return 'secondaryWeapon';
      case EquipmentType.KNIFE:
        return 'knife';
      case EquipmentType.BACKPACK:
        return 'backpack';
      case EquipmentType.AMMO_POUCH:
        return 'ammoPouch';
      default:
        // 如果是字符串值，尝试映射
        const stringType = slotType as string;
        if (stringType === 'primary_weapon') return 'primaryWeapon';
        if (stringType === 'secondary_weapon') return 'secondaryWeapon';
        if (stringType === 'ammo_pouch') return 'ammoPouch';
        if (stringType === 'backpack') return 'backpack';
        throw new Error(`Unknown equipment type: ${slotType}`);
    }
  }

  /**
   * 获取装备的显示名称
   */
  static getSlotDisplayName(slotType: EquipmentType): string {
    switch (slotType) {
      case EquipmentType.HEAD:
        return '头部装备';
      case EquipmentType.BODY:
        return '身体装备';
      case EquipmentType.PRIMARY_WEAPON:
        return '主武器';
      case EquipmentType.SECONDARY_WEAPON:
        return '副武器';
      case EquipmentType.KNIFE:
        return '刀';
      case EquipmentType.BACKPACK:
        return '背包';
      case EquipmentType.AMMO_POUCH:
        return '弹挂';
      default:
        return '未知装备';
    }
  }

  /**
   * 检查装备的总价值
   */
  static calculateEquipmentValue(equipment: EquipmentSlots): number {
    return Object.values(equipment).reduce((total, item) => {
      return total + (item?.baseValue || 0);
    }, 0);
  }

  /**
   * 获取所有可用的装备类型
   */
  static getAllEquipmentTypes(): EquipmentType[] {
    return Object.values(EquipmentType);
  }
}